<?php
namespace app\admin\controller;

use think\Controller;

class CategoryWords extends Controller
{
    public function lst()
    {

        $category_wordsRes = db('category_words')->field('cw.*,c.cate_name')->alias('cw')->join('category c', "cw.category_id = c.id")->paginate(10);
        $this->assign([
            'category_wordsRes' => $category_wordsRes,
        ]);
        return view('list');
    }

    public function add()
    {
        if (request()->isPost()) {

            $data = input('post.');

            if ($data['link_url'] && stripos($data['link_url'], 'http://') === false) {
                $data['link_url'] = 'http://' . $data['link_url'];
            }

            $add = db('category_words')->insert($data);

            if ($add) {

                $this->success('添加关联词成功 ！', 'lst');
            } else {

                $this->error('添加关联词失败');
            }
            return;
        }
        $cateRes = model('Category')->where(array('pid' => 0))->select();

        $this->assign([
            'cateRes' => $cateRes,

        ]);

        return view();
    }
    public function edit()
    {

        if (request()->isPost()) {
            $data = input('post.');

            if ($data['link_url'] && stripos($data['link_url'], 'http://') === false) {
                $data['link_url'] = 'http://' . $data['link_url'];
            }

            $save = db('category_words')->update($data);
            if ($save !== false) {

                $this->success('修改关联词成功 ！', 'lst');
            } else {

                $this->error('修改关联词失败');
            }
            return;
        }

        $id = input('id');

        $category_words = db('category_words')->find($id);
        $cateRes        = model('Category')->where(array('pid' => 0))->select();

        $this->assign([
            'category_words' => $category_words,
            'cateRes'        => $cateRes,

        ]);

        return view();
    }
    public function del($id)
    {
        $del = db('category_words')->delete($id);

        if ($del) {

            $this->success('删除关联词成功 ！', 'lst');
        } else {

            $this->error('删除关联词失败');
        }
    }

}
